//
//  FSLContentView.swift
//  FSLText
//
//  Created by 我演示 on 2019/11/2.
//  Copyright © 2019 ALLSall. All rights reserved.
//

import UIKit

private let kContentCellId = "kContentCellId"
protocol FSLContentViewDelegate: class {
    func contentView(_ contentView : FSLContentView , targetIndex : Int)
    
    //2: 创建滑动效果的view
    func contentView (_ contentView : FSLContentView ,targetIndex : Int , progress : CGFloat)
}

class FSLContentView: UIView {

    weak var delegate : FSLContentViewDelegate?
    fileprivate var startOffsex : CGFloat = 0.0
    fileprivate var isForbidScroll : Bool = false
    
    lazy var collectionView : UICollectionView = {
        
        let layout = UICollectionViewLayout.init()
       let collectionView = UICollectionView.init(frame: CGRect(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height), collectionViewLayout: layout)
        
        collectionView.dataSource = self
        collectionView.delegate = self
        
        return collectionView
        
    }()
    var parentVc : UIViewController
    var childVcs : [UIViewController]
    
    
    init(frame: CGRect , childVcs : [UIViewController] , parentVc : UIViewController ){
        
        self.childVcs = childVcs
        self.parentVc = parentVc
        
        super.init(frame: frame)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


extension FSLContentView : UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "string", for: indexPath)
        for subView in cell.contentView.subviews {
            subView.removeFromSuperview()
        }
        let childVc = childVcs[indexPath.item]
         childVc.view.frame = cell.contentView.bounds
        cell.contentView.addSubview(childVc.view)
        return cell
        
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
         return childVcs.count
    }
    
}

extension FSLContentView : UICollectionViewDelegate {
    /// 对事件进行监听
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        if !decelerate {
            contentEendScroll()
        }
        
    }
    /// 没有渐变过程的处理
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        contentEendScroll()
    }
    
    private func contentEendScroll() {
        
        //0: 判断是否是禁止状态
        guard  !isForbidScroll  else {return }
        
        //1`: 获取滚动位置的
        let currentIndex = Int(collectionView.contentOffset.x / collectionView.bounds.size.width)
        
        // 2: 通知titleVIew 遵守协议和代理
        delegate?.contentView(self, targetIndex: currentIndex)
        
        //3: 让contentView 成为代理
    }
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        //0: 判断是否是禁止状态
        guard  !isForbidScroll  else {return }
        
        startOffsex = scrollView.contentOffset.x
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //0: 判断和我开始时的偏移量是不是一致
        guard startOffsex != scrollView.contentOffset.x  else {
            return
        }
        
        /**1:定义targetIndex  progress*/
        
        var targetIndex = 0
        var progress : CGFloat = 0.00
        
        /**2: 给targeiIndex progress 赋值*/
          let currentIndex = Int (startOffsex / scrollView.bounds.width)
        if startOffsex < scrollView.contentOffset.x { // 用户左滑
            targetIndex = currentIndex + 1
            if targetIndex > childVcs.count - 1 {
                targetIndex = childVcs.count - 1
            }
            progress = (scrollView.contentOffset.x - startOffsex) / scrollView.bounds.width
            
        }else {
             targetIndex = currentIndex - 1
            if targetIndex < 0 {
                targetIndex = 0
            }
            progress = (startOffsex - scrollView.contentOffset.x   ) / scrollView.bounds.width
            
        }
        
        //3: 通知代理
        delegate?.contentView(self, targetIndex: targetIndex, progress: progress)
        
        
    }
    
    
}


/// 遵守协议和代理

extension FSLContentView : FSLPageTitleViewDelegate {
    func pageView(_ pageView: FSLTitleView, targetIndex: Int) {
        
        isForbidScroll = true
        
        let indexPath = IndexPath(item: targetIndex, section: 0)
        collectionView.scrollToItem(at: indexPath, at: .left, animated: false)
        
    }
    
    
    
}
